<?php

namespace App\Repositories\Impl;

use App\Models\BranchInfo;
use App\Repositories\Contracts\BranchInfoRepository;
use App\Repositories\Transformers\BranchInfoListTransformer;
use App\Repositories\Transformers\BranchInfoTransformer;

class BranchInfoRepositoryImpl implements BranchInfoRepository
{
    public function paginate($perPage, $filter)
    {
        if (!empty($filter['branch_id'])) {
            $branchInfo = BranchInfo::with('branch')->where('branch_id', $filter['branch_id'])->orderBy('created_at', 'desc')->paginate($perPage);
        } else {
            $branchInfo = BranchInfo::with('branch')->orderBy('created_at', 'desc')->paginate($perPage);
        }

        $result = fractal($branchInfo, new BranchInfoListTransformer());

        return $result->toArray();

    }

    public function find($id, $columns = ['*'])
    {
        $branchInfo = BranchInfo::find($id, $columns);
        $result = fractal($branchInfo, new BranchInfoTransformer());

        return $result->toArray();

    }

    public function findByFilter($filter, $columns = ['*'])
    {
        return BranchInfo::where($filter)->first($columns);
    }

    public function store($data)
    {
        return BranchInfo::create($data);
    }

    public function update($data, $id)
    {
        return BranchInfo::whereId($id)->update($data);
    }

    public function updateByFilter($data, $where)
    {
        return BranchInfo::where($where)->update($data);
    }

    public function delete($id)
    {
        return BranchInfo::whereId($id)->delete();
    }
}
